<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 *
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 *
 * @package      Feather's website
 * @copyright    Copyright (c) 2006, Julien PORTALIER
 * @link         http://feather.portalier.com
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

abstract class CommentsController extends Controller
{
	public $_templates = 'simple_comments';
	
	public $refererClass;
	
	// display
	
	function index($referer_id)
	{
		$model   = $this->{$this->modelClass};
		$referer = $model->{$this->refererClass};
		
		// conditions
		if (!$this->Auth->inGroup(Config::who_can_blog))
		{
			$model->conditions(array(
				"{$this->modelClass}.published" => PUBLISH_ONLINE,
				"{$this->modelClass}.created"   => '-! < NOW()'
			));
		}
		$model->conditions(array("{$this->modelClass}.{$referer->foreignKey}" => $referer_id));
		$model->order("{$this->modelClass}.created ASC");
		$model->recursive = 0;
		
		// comments
		$this->data =& $model->findAll();
	}
	
	function rss($referer_id)
	{
		$this->index($referer_id);
	}
	
	// admin
	
	function create($id='void', $referer_id)
	{
		$model   = $this->{$this->modelClass};
		$referer = $model->{$this->refererClass};
		
		// has user rights to post a comment?
		$referer->recursive = -1;
		$referer->process   = Q_PROCESS_LITE;
		$tmp = $referer->read("published, allow_comments", $referer_id);
		
		if (empty($tmp)) {
			$this->flash(_("An error occured: there is no such resource."));
		}
		elseif (!$tmp['allow_comments']) {
			$this->flash(_("An error occured: comments are closed for this resource."), $this->url($id));
		}
		elseif ($tmp['published'] != PUBLISH_ONLINE and !$this->Auth->inGroup(Config::who_can_blog)) {
			$this->Auth->permissionDenied();
		}
		
		if (empty($this->data))
		{
			// loads data
			$this->data = $model->read();
			
			if (empty($this->data))
				Error::show(404);
		}
		else
		{
			// tries to save
			if ($model->save())
				$this->flash(_('The comment has been published.'), $this->url());
			else
				$this->Session->setFlash(_('Oops, an error occured.'));
		}
		
		$this->pageTitle = _('Comment creation');
	}
	
	function edit($id)
	{
		$model = $this->{$this->modelClass};
		
		if (empty($this->data))
		{
			// loads data
			$this->data = $model->read();
			
			if (empty($this->data))
				Error::show(404);
		}
		
		// has user rights over comment?
		if (!$this->Auth->identified
			or $this->Auth->id != $this->data['Member']['id']
			or !$this->Auth->inGroup(Config::who_can_blog))
		{
			$this->Auth->permissionDenied();
		}
		
		if (isPost())
		{
			// tries to save
			if ($model->save())
				$this->flash(_('The comment has been published.'), $this->url());
			else
				$this->Session->setFlash(_('Oops, an error occured.'));
		}
		
		$this->pageTitle = _('Comment edition');
	}
	
	function delete($id)
	{
		// has user rights over data?
		if (!$this->Auth->inGroup(Config::who_can_blog))
			$this->Auth->permissionDenied();
		
		// tries to delete
		if ($this->{$this->modelClass}->delete())
			$this->Session->setFlash(_('The comment has been deleted.'));
		else
			$this->Session->setFlash(_('Oops, an error occured.'));
		
		$this->redirect(getReferer());
	}
	
	abstract function url(&$id=null);
}
?>